﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class FKSZModule : BaseModule
    {
        public FKSZModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            this.tsBtnNew.Visible = false;
            this.tsBtnDelete.Visible = false;
            this.tsBtnImportExcel.Visible = false;
            this.tsBtnDownTemplate.Visible = false;
            this.tsBtnSave.Enabled = false;
        }

        protected override void OnInit()
        {
            base.OnInit();
            SetRadioBtnEnabled(false);
        }

        private void SetRadioBtnEnabled(bool bEnabled)
        {
            this.radioBtnYes.Enabled = bEnabled;
            this.radioBtnNo.Enabled = bEnabled;
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();

            this.radioBtnNo.Checked = true;
            DataTable dtContent = DataAccess.ExecuteDataTable("select WGZFK from T_SAFETY_INFORM_SETTING");
            if (dtContent != null && dtContent.Rows.Count > 0 && dtContent.Rows[0]["WGZFK"].ToString() == "1")
            {
                this.radioBtnYes.Checked = true;
            }
        }

        protected override void OnModify()
        {
            base.OnModify();
            if (tsBtnModify.Enabled)
            {
                tsBtnModify.Enabled = false;
                tsBtnRefresh.Enabled = false;
                tsBtnSave.Enabled = true;

                SetRadioBtnEnabled(true);
            }
        }

        protected override void OnSave()
        {
            base.OnSave();

            string strWGZFK = radioBtnYes.Checked ? "1" : "0";
            string strSql = "update T_SAFETY_INFORM_SETTING set WGZFK = :WGZFK, WGZFK_XGR = :WGZFK_XGR, WGZFK_XGSJ = :WGZFK_XGSJ";
            DataAccess.ExecuteNonQuery(strSql, new OracleParameter(":WGZFK", strWGZFK),
                new OracleParameter(":WGZFK_XGR", Session.CurrentUserName), new OracleParameter(":WGZFK_XGSJ", DateTime.Now));

            MessageBox.Show("保存成功！", "确认");
        }

        protected override void OnAfterSave()
        {
            base.OnAfterSave();
            if (tsBtnSave.Enabled)
            {
                tsBtnSave.Enabled = false;
                tsBtnRefresh.Enabled = true;
                tsBtnModify.Enabled = true;

                SetRadioBtnEnabled(false);
            }
        }

        protected override void OnBeforeClose(ref bool _canClose)
        {
            base.OnBeforeClose(ref _canClose);
            if (tsBtnSave.Enabled)
            {
                if (MessageBox.Show("修改尚未保存，是否关闭？", "确认", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    _canClose = false;
                }
            }
        }
    }
}
